package com.recruit.company.register;

import javax.servlet.http.HttpServletRequest;

import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.PasswordUtil;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.web.system.pojo.base.TSBaseUser;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.jeecgframework.web.system.pojo.base.TSRole;
import org.jeecgframework.web.system.pojo.base.TSRoleUser;
import org.jeecgframework.web.system.pojo.base.TSUser;
import org.jeecgframework.web.system.pojo.base.TSUserOrg;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/companyRegisterController")
public class CompanyRegisterController {
	
	@Autowired
	private SystemService systemService;
	
	/**
	 * 跳转到企业账号注册
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "toRegister")
	public ModelAndView uitag(HttpServletRequest request) {
		return new ModelAndView("com/recruit/company/register");
	}
	
	
	/**
	 * 企业账号注册
	 * @param userName
	 * @param realName
	 * @param password
	 * @param orgIds
	 * @param roleid
	 * @param devFlag
	 * @return
	 */
	@RequestMapping(params = "doRegister")
	public ModelAndView doRegister(ModelMap map, String userName,String realName,String password,String orgIds,String roleid,String devFlag){
		
		TSBaseUser checkUser = systemService.findUniqueByProperty(TSBaseUser.class, "userName", userName);
		if(StringUtil.isNotEmpty(checkUser)){
			map.addAttribute("userName", userName);
			map.addAttribute("realName", realName);
			
			map.addAttribute("msg", 1);//用户已被注册
			return new ModelAndView("com/recruit/company/register");
		}else{
			//注册企业用户信息
			TSUser user = new TSUser();
			user.setUserName(userName);
			user.setRealName(realName);
			user.setPassword(PasswordUtil.encrypt(user.getUserName(), password, PasswordUtil.getStaticSalt()));
			user.setStatus(Globals.User_Normal);
			user.setDeleteFlag(Globals.Delete_Normal);
			user.setDevFlag("0");
			user.setDepartid(orgIds);
			systemService.save(user);
			//设置用户角色权限
			TSRole role = systemService.get(TSRole.class, "402880e661e0b5000161e0c220c80010");
			TSRoleUser roleuser = new TSRoleUser();
			roleuser.setTSRole(role);
			roleuser.setTSUser(user);
			systemService.save(roleuser);
			//设置用户机构
			TSDepart tsDepart = systemService.get(TSDepart.class, "8a8ab0b246dc81120146dc8180ba0017");
			TSUserOrg userorg = new TSUserOrg();
			userorg.setTsDepart(tsDepart);
			userorg.setTsUser(user);
			systemService.save(userorg);
			map.addAttribute("msg", 2);//注册成功
			return new ModelAndView("com/recruit/company/register");
		}
		
		
	}
	
}
